package com.simon.jkxj.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.simon.jkxj.R;
import com.simon.jkxj.model.EncyclopediaNews;

import java.util.Arrays;
import java.util.List;

public class EncyclopediaNewsAdapter extends RecyclerView.Adapter<EncyclopediaNewsAdapter.NewsViewHolder> {

    private List<EncyclopediaNews> newsList;
    private Context context;
    private OnItemClickListener listener;

    public interface OnItemClickListener {
        void onItemClick(EncyclopediaNews news, int position);
    }

    public EncyclopediaNewsAdapter(Context context, List<EncyclopediaNews> newsList) {
        this.context = context;
        this.newsList = newsList;
    }

    public EncyclopediaNewsAdapter(Context context) {
        this.context = context;
        this.newsList = Arrays.asList(EncyclopediaNews.getMockData());
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    public NewsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_encyclopedia_news, parent, false);
        return new NewsViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull NewsViewHolder holder, int position) {
        EncyclopediaNews news = newsList.get(position);
        
        holder.titleTextView.setText(news.getTitle());
        holder.contentTextView.setText(news.getContent());
        holder.sourceTextView.setText(news.getSource());
        holder.timeTextView.setText(news.getTime());
        
        // 所有新闻条目都使用相同的图片资源
        holder.newsImageView.setImageResource(R.drawable.news);
        
        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onItemClick(news, position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return newsList.size();
    }

    public void updateData(List<EncyclopediaNews> newsList) {
        this.newsList = newsList;
        notifyDataSetChanged();
    }

    static class NewsViewHolder extends RecyclerView.ViewHolder {
        ImageView newsImageView;
        TextView titleTextView;
        TextView contentTextView;
        TextView sourceTextView;
        TextView timeTextView;

        public NewsViewHolder(@NonNull View itemView) {
            super(itemView);
            newsImageView = itemView.findViewById(R.id.iv_news_image);
            titleTextView = itemView.findViewById(R.id.tv_news_title);
            contentTextView = itemView.findViewById(R.id.tv_news_content);
            sourceTextView = itemView.findViewById(R.id.tv_news_source);
            timeTextView = itemView.findViewById(R.id.tv_news_time);
        }
    }
} 